package com.huacai.homestay.service.impl;

import java.util.Collections;
import java.util.List;

import com.huacai.common.annotation.DataScope;
import com.huacai.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.huacai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.huacai.homestay.domain.HomestayTags;
import com.huacai.homestay.mapper.HomestayMapper;
import com.huacai.homestay.domain.Homestay;
import com.huacai.homestay.service.IHomestayService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import static com.huacai.common.utils.SecurityUtils.getUserId;

/**
 * 酒店Service业务层处理
 *
 * @author hsj
 * @date 2025-05-14
 */
@Service
public class HomestayServiceImpl implements IHomestayService
{
    @Autowired
    private HomestayMapper homestayMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询酒店
     *
     * @param homestayId 酒店主键
     * @return 酒店
     */
    @Override
    public Homestay selectHomestayByHomestayId(String homestayId)
    {
        return homestayMapper.selectHomestayByHomestayId(homestayId);
    }

    /**
     * 查询酒店列表
     *
     * @param homestay 酒店
     * @return 酒店
     */
    @Override
    @DataScope(userAlias = "t1")  //主表名为t1
    public List<Homestay> selectHomestayList(Homestay homestay)
    {
        return homestayMapper.selectHomestayList(homestay);
    }

    /**
     * 新增酒店
     *
     * @param homestay 酒店
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHomestay(Homestay homestay)
    {
        //酒店ID
        homestay.setHomestayId(IdUtils.fastSimpleUUID());
        //userID
        homestay.setUserId(Math.toIntExact(getUserId()));
        int rows = homestayMapper.insertHomestay(homestay);
        insertHomestayTags(homestay);
        return rows;
    }

    /**
     * 批量新增酒店
     *
     * @param homestays 酒店List
     * @return 结果
     */
    @Override
    public int batchInsertHomestay(List<Homestay> homestays)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(homestays)) {
            try {
                for (int i = 0; i < homestays.size(); i++) {
                    int row = homestayMapper.insertHomestay(homestays.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == homestays.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改酒店
     *
     * @param homestay 酒店
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHomestay(Homestay homestay)
    {
        homestayMapper.deleteHomestayTagsByHomestayId(homestay.getHomestayId());
        insertHomestayTags(homestay);
        return homestayMapper.updateHomestay(homestay);
    }

    /**
     * 批量删除酒店
     *
     * @param homestayIds 需要删除的酒店主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHomestayByHomestayIds(String[] homestayIds)
    {
        homestayMapper.deleteHomestayTagsByHomestayIds(homestayIds);
        return homestayMapper.deleteHomestayByHomestayIds(homestayIds);
    }

    /**
     * 删除酒店信息
     *
     * @param homestayId 酒店主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHomestayByHomestayId(String homestayId)
    {
        homestayMapper.deleteHomestayTagsByHomestayId(homestayId);
        return homestayMapper.deleteHomestayByHomestayId(homestayId);
    }


    /**
     * 通过景点ID查询附近酒店列表
     */
    @Override
    public List<Homestay> selectHomestayBySpotsId(String spotsId) {
        return homestayMapper.selectHomestayBySpotsId(spotsId);
    }

    /**
     * 通过餐厅ID查询附近酒店列表
     */
    @Override
    public List<Homestay> selectHomestayByRestaurantId(Integer restaurantId) {
        return homestayMapper.selectHomestayByRestaurantId(restaurantId);
    }


    /**
     * 查询特色酒店列表和酒店标签
     */
    @Override
    public List<Homestay> selectHomestayOrTagsList(Homestay homestay) {
        return homestayMapper.selectHomestayOrTagsList(homestay);
    }

    /**
     * 新增酒店标签信息
     *
     * @param homestay 酒店对象
     */
    public void insertHomestayTags(Homestay homestay)
    {
        List<HomestayTags> homestayTagsList = homestay.getHomestayTagsList();
        String homestayId = homestay.getHomestayId();
        if (StringUtils.isNotNull(homestayTagsList))
        {
            List<HomestayTags> list = new ArrayList<HomestayTags>();
            for (HomestayTags homestayTags : homestayTagsList)
            {
                homestayTags.setHomestayId(homestayId);
                list.add(homestayTags);
            }
            if (list.size() > 0)
            {
                homestayMapper.batchHomestayTags(list);
            }
        }
    }
}
